import { cloneDeep } from 'lodash-es'

// 根据用户角色列表递归过滤出对应的路由
export function recurseFilterRoute(routes: any[], roleList: string[]) {
  const tempArr = routes
    .map((node) => ({ ...node }))
    .filter((node) => {
      const { meta } = node
      const { roles } = meta || {}
      if (!roles) return true
      return roleList.some((role) => roles.includes(role))
    })
  for (const item of tempArr) {
    if (item.children && item.children.length) {
      item.children = recurseFilterRoute2(item.children, roleList)
    }
  }
  return tempArr
}

// 根据用户角色列表递归过滤出对应的路由，写法2
export function recurseFilterRoute2(routes: any[], roleList: string[]) {
  return routes.filter((item) => {
    if (roleList.find((it) => item?.meta?.roles?.indexOf(it) !== -1)) {
      if (item.children && item.children.length) {
        item.children = recurseFilterRoute(item.children, roleList)
      }
      return true
    }
  })
}

// 加载对应的component
function loadComponent(component) {
  if (component && component === 'empty') {
    return () => import('@/layout/cnps/empty/empty.vue')
  } else {
    return () => import(/* webpackChunkName: "[request]" */ `@/views${component}.vue`)
  }
}

// 根据后台获取到的用户菜单转化为routes
export function transformObjToRoute(backMenus: any[]) {
  backMenus.forEach((menu) => {
    menu.component = loadComponent(menu.component)

    if (menu.children && menu.children.length) {
      transformObjToRoute(menu.children)
    }
  })

  return backMenus
}

/*
  路由扁平化，将多级路由拉平，解决keep-alive组件在多级路由情况下不生效的问题。
  路由拉平的同时，将会导致面包屑没有层级，还需对面包屑进行处理
*/
export function flatMultiLevelRoutes(routes: any[]): any[] {
  const cloneRoutes = cloneDeep(routes)
  const newRoutes: any[] = []

  // 处理面包屑问题
  // 1、先递归给每个路由添加meta.breadcrumbList
  function routeAddBreadcrumb(arr) {
    arr.forEach((item) => {
      item.meta.breadcrumbList = []

      item.meta.breadcrumbList.push({
        title: item.meta.title,
        path: item.path,
        children: item.children || []
      })

      if (item.children && item.children.length) {
        routeAddBreadcrumb(item.children)
      }
    })
  }
  routeAddBreadcrumb(cloneRoutes)

  // 2、递归合并父子路由的meta.breadcrumbList
  function concatFatherAndSonBreadcrumbList(arr) {
    arr.forEach((item) => {
      if (item.children && item.children.length) {
        item.children.forEach((cItem) => {
          cItem.meta.breadcrumbList = item.meta.breadcrumbList.concat(cItem.meta.breadcrumbList)
        })

        concatFatherAndSonBreadcrumbList(item.children)
      }
    })
  }
  concatFatherAndSonBreadcrumbList(cloneRoutes)

  // 拉平路由
  function recurseHandleRoute(arr) {
    arr.forEach((item) => {
      if (item.children && item.children.length) {
        item.children.forEach((cItem) => {
          // 只处理meta.type为menu和iframe的路由，meta.type为link的不能处理
          if (cItem.meta.type !== 'link') {
            cItem.path = `${item.path}/${cItem.path}`
          }
        })
        recurseHandleRoute(item.children)
      } else {
        newRoutes.push(item)
      }
    })
  }
  recurseHandleRoute(cloneRoutes)

  return newRoutes
}
